﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;

namespace WinFormsApp1
{
    public partial class AddDepartmentData : Form
    {
        public AddDepartmentData()
        {
            InitializeComponent();
            //执行获取企业表数据
            AddBusinessSelectData();
            
        }
        /// <summary>
        /// 给企业下拉框添加数据
        /// </summary>
        public async void AddBusinessSelectData()
        {
            //http://localhost:5106/api/Tsw/ShowSectionData
            //设置页面下拉框的值
            List<Business> data = new List<Business>()
            { 
                //给下拉框添加初始值
                new Business{ BusinessId=0 ,BusinessName="请选择企业"}
            };
            var res = await HttpClientHelper.RequestUrl<Business, ApiResult<List<Business>>>(URLCommon.Url2, "/TSW/ShowSectionData", HttpClientHelper.MethodType.GET);
            if (res.Code > 0)
            {
                data.AddRange(res.Data);
            }
            //存储页面下拉框的值
            GetBusinessId.DataSource = data;
            GetBusinessId.DisplayMember = "BusinessName"; // 显示的成员
            GetBusinessId.ValueMember = "BusinessId";     // 主键值的成员
            //添加下拉框
            Controls.Add(GetBusinessId);
        }
        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void button1_Click(object sender, EventArgs e)
        {
            //部门名称
            var SectionName = GetSectionName.Text.ToString();
            //部门类型
            var SectionType = GetSectionType.Text.ToString();
            //企业管理
            var BusinessId = Convert.ToInt32(GetBusinessId.SelectedValue);
            //判断初始值，假如是初始值就直接退出
            if (BusinessId == 0)
            {
                MessageBox.Show("请选择企业管理后再添加");
                return;
            }
            //是否删除
            var IsDelete = false;
            //http://localhost:5159/api/Week01/AddSectionData
            //定义获取结构数据
            Sections sections = new(SectionName, SectionType, BusinessId, IsDelete);
            //执行添加操作
            var res = await HttpClientHelper.RequestUrl<Sections, ApiResult<Sections>>(URLCommon.Url1, "/Week01/AddSectionData", HttpClientHelper.MethodType.POST, sections);
            if (res.Code > 0)
            {
                MessageBox.Show("添加部门成功");
                return;
            }
            else
            {
                MessageBox.Show("添加部门失败");
                return;
            }
        }

        private void GetBusinessId_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 定义结构
        /// </summary>
        public record Sections(string SectionName, string SectionType, int BusinessId, bool IsDelete);
        /// <summary>
        /// 定义企业下拉框数据
        /// </summary>
        public class Business
        {
            /// <summary>
            /// 企业编号
            /// </summary>
            [Key]
            public int BusinessId { get; set; }
            /// <summary>
            /// 企业名称
            /// </summary>
            public string BusinessName { get; set; }
            /// <summary>
            /// 企业简称
            /// </summary>
            public string BusinessAbbreviation { get; set; }
            /// <summary>
            /// 企业LOGO
            /// </summary>
            public string BusinessImage { get; set; }
            /// <summary>
            /// 行业标签(外键)
            /// </summary>
            public int IndustrytagId { get; set; }
            /// <summary>
            /// 省(外键)
            /// </summary>
            public int ProvibceId { get; set; }
            /// <summary>
            /// 市区(外键)
            /// </summary>
            public int CityId { get; set; }
            /// <summary>
            /// 区(外键)
            /// </summary>
            public int AreaId { get; set; }
            /// <summary>
            /// 详情地址
            /// </summary>
            public string DetailedAddress { get; set; }
            /// <summary>
            /// 企业电话
            /// </summary>
            public string BusinessPhone { get; set; }
            /// <summary>
            /// 负责人
            /// </summary>
            public string BusinessDuty { get; set; }
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsDelete { get; set; }
        }

        private void AddDepartmentData_Load(object sender, EventArgs e)
        {

        }
    }
}
